package com.atguigu.securitydemo1.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 方式二:配置类的形式(实际开发中不常用)
 * 配置登录用户名和密码验证
 * 1.需要先继承WebSecurityConfigurerAdapter
 */
// @Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    /**
     * 2.重写这个方法来做用户名和密码效验
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode("123");

        //设置用户名和密码信息
        auth.inMemoryAuthentication()
                .withUser("lucy")//用户名
                .password(password)//密码
                .roles("admin");//角色
    }


    /**
     * BCryptPasswordEncoder上面的BCryptPasswordEncoder类会报找不到继承接口的异常,需要将接口注入到容器中
     * @return
     */
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
